<?php

namespace app\admin\controller\family;

use app\common\controller\Backend;
use think\Db;

/**
 * 家庭成员管理
 *
 * @icon fa fa-circle-o
 */
class Members extends Backend
{

    /**
     * Home模型对象
     * @var \app\admin\model\family\Home
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Admin;
    }

    public function index()
    {
        $group = $this->auth->getGroups();
        $user = $this->auth->getUserInfo();
        $groupId = $group[0]["group_id"];
        $this->assign('groupId', $groupId);
        $this->assignconfig('adid', $user["id"]);
        if ($groupId == 1) {
            if ($this->request->isAjax()) {
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
                $result = array("total" => $list->total(), "rows" => $list->items());
                return json($result);
            }
        } else if ($groupId == 2) {
            if ($this->request->isAjax()) {

                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                $list = $this->model
                    ->where($where)
                    ->where('home_id', $user["home_id"])
                    ->order($sort, $order)
                    ->paginate($limit);
                $result = array("total" => $list->total(), "rows" => $list->items());
                return json($result);
            }
        }
        return $this->view->fetch();
    }

    public function tuichu($ids)
    {
        $admin = $this->auth->getUserInfo();
        $user = $this->model->get(['id' => $ids])->toArray();
        $row = Db::table('jc_family_home')->where('id' , $admin["home_id"])->select();
        $row=$row[0];
        $access = array("group_id" => 4);
        $accesstui = array("group_id" => 3);
        $home_id = array("home_id" =>  null);
        $home_idtui = array("home_id" => $row["id"]);
        $home_size = array(
            "home_size" => $row["home_size"] - 1
        );
        $accessEdit = Db::table('jc_auth_group_access')->where("uid", $user['id'])->update($access);
        if ($accessEdit) {
            $adminEdit =  Db::table('jc_admin')->where("id", $user['id'])->update($home_id);
            if ($adminEdit) {
                $homeEdit = Db::table('jc_family_home')->where('id' , $admin["home_id"])->update($home_size);
                if ($homeEdit) {
                    $this->success('退出成功');
                } else {
                    $adminEdit =  Db::table('jc_admin')->where("id", $user['id'])->update($home_idtui);
                    $this->error('退出失败');
                }
            } else {
                $accessEdit = Db::table('jc_auth_group_access')->where("uid", $user['id'])->update($accesstui);
                $this->error('退出失败');
            }
        } else {
            $this->error('退出失败');
        }
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
}
